package com.zhq.controller;

import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowGrantsStatement;
import com.zhq.bean.*;
import com.zhq.mvc.ResponseBody;
import com.zhq.service.ExpressService;
import com.zhq.service.UserService;
import com.zhq.util.DateFormatUtil;
import com.zhq.util.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author:钟豪强
 * @Description:    处理user有关的请求
 * @Date Created in  2020-08-18 18:17
 * @Modified By:
 */
public class UserController {

    @ResponseBody("/user/console.do")
    public String console(HttpServletRequest request, HttpServletResponse response){
        Map<String, Integer> data = UserService.console();
        Message msg = new Message();
        if (data.size() == 0){
            msg.setStatus(-1);
        }else{
            msg.setStatus(0);
        }
        msg.setData(data);
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/user/list.do")
    public String list(HttpServletRequest request, HttpServletResponse response){
        System.out.println("接收到请求了你妈的");
        //1.    获取查询数据的起始索引值
        int offset = Integer.parseInt(request.getParameter("offset"));
        //2.    获取当前页要查询的数据量
        int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
        List<User> list = UserService.findAll(true, offset, pageNumber);
        ArrayList<BootStrapTableUser> list1 = new ArrayList<>();
        for (User user:list) {
            int id = user.getId();
            String username = user.getUsername();
            String userPhone = user.getUserPhone();
            String password = user.getPassword();
            String createTime = DateFormatUtil.format(user.getCreateTime());
            String lastLoginTime = user.getLastLoginTime() == null ? "" : DateFormatUtil.format(user.getLastLoginTime());
            BootStrapTableUser bootStrapTableUser = new BootStrapTableUser(id,username,userPhone,password,createTime,lastLoginTime);
            list1.add(bootStrapTableUser);
        }
        Map<String, Integer> console = UserService.console();
        Integer total = console.get("data_size");
        //将集合封装成bootstrap能识别的格式
        ResultData<BootStrapTableUser> data = new ResultData<>();
        data.setTotal(total);
        data.setRows(list1);
        String json = JSONUtil.toJSON(data);
        return json;
    }

    @ResponseBody("/user/insert.do")
    public String insert(HttpServletRequest request, HttpServletResponse response){
        System.out.println("收到你妈的请求");
        //1.    接收参数
        String username = request.getParameter("username");
        String userPhone = request.getParameter("userPhone");
        String userNumber = request.getParameter("userNumber");
        String password = request.getParameter("password");
        boolean flag = UserService.insert(username, userPhone, userNumber, password);
        Message msg = new Message();
        if (flag) {
            msg.setStatus(0);
            msg.setResult("添加成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("添加失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/user/find.do")
    public String find(HttpServletRequest request, HttpServletResponse response){
        String userPhone = request.getParameter("userPhone");
        User user = UserService.findByPhone(userPhone);
        Message msg = new Message();
        if (user != null){
            msg.setStatus(0);
            msg.setData(user);
        }else{
            msg.setStatus(-1);
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/user/update.do")
    public String update(HttpServletRequest request, HttpServletResponse response){
        System.out.println("启动");
        String username = request.getParameter("username");
        String userPhone = request.getParameter("userPhone");
        String userNumber = request.getParameter("userNumber");
        String password = request.getParameter("password");
        String g_userPhone = request.getParameter("g_userPhone");
        int id = Integer.parseInt(request.getParameter("userId"));
        User newUser = new User();
        newUser.setUsername(username);
        newUser.setUserPhone(userPhone);
        newUser.setUserNumber(userNumber);
        newUser.setPassword(password);
        newUser.setG_userPhone(g_userPhone);
        boolean flag = UserService.update(id, newUser);
        Message msg = new Message();
        if (flag) {
            msg.setStatus(0);
            msg.setResult("修改成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("修改失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/user/delete.do")
    public String delete(HttpServletRequest request, HttpServletResponse response){
        //1.接收参数
        int id = Integer.parseInt(request.getParameter("id"));
        boolean flag = UserService.delete(id);
        Message msg = new Message();
        if (flag) {
            msg.setStatus(0);
            msg.setResult("删除成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("删除失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/user/checkName.do")
    public String checkName(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        List<User> list = UserService.findAll(false, 0, 0);
        Message msg = new Message();
        msg.setStatus(0);
        for (User u:list) {
            if (username.equals(u.getUsername())) {
                msg.setStatus(-1);
                msg.setResult("该用户名已被使用");
            }
        }
        return JSONUtil.toJSON(msg);
    }

    @ResponseBody("/user/checkPhone.do")
    public String checkPhone(HttpServletRequest request, HttpServletResponse response){
        String userPhone = request.getParameter("userPhone");
        List<User> list = UserService.findAll(false, 0, 0);
        Message msg = new Message();
        msg.setStatus(0);
        for (User u:list) {
            if (userPhone.equals(u.getUserPhone())) {
                msg.setStatus(-1);
                msg.setResult("该手机号码已存在");
            }
        }
        return JSONUtil.toJSON(msg);
    }

    @ResponseBody("/user/checkNumber.do")
    public String checkNumber(HttpServletRequest request, HttpServletResponse response){
        String userNumber = request.getParameter("userNumber");
        List<User> list = UserService.findAll(false, 0, 0);
        Message msg = new Message();
        msg.setStatus(0);
        for (User u:list) {
            if (userNumber.equals(u.getUserNumber())) {
                msg.setStatus(-1);
                msg.setResult("该身份证已被使用");
            }
        }
        return JSONUtil.toJSON(msg);
    }
}
